package starcoast.bocai.com.starcoast.activitys.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.adapter.PurchaseAdapter;
import starcoast.bocai.com.starcoast.bean.ADInfo;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.util.ViewFactory;
import starcoast.bocai.com.starcoast.view.CycleViewPager;
import starcoast.bocai.com.starcoast.view.MyGridView;

/**
 * Created by admin on 2016/6/13.
 */
public class PurchaseWorkActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.purchase_office)
    MyGridView purchase_office;
    @Bind(R.id.purchase_electronic)
    MyGridView purchase_electronic;
    PurchaseAdapter adapter;
    CycleViewPager cycleViewPager;
    private List<ImageView> views = new ArrayList<ImageView>();
    private List<ADInfo> infos = new ArrayList<ADInfo>();
    private List<String> imageList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        ButterKnife.bind(this);
        initToolbar("急速采购");
        initToolBack();
        toolbar.inflateMenu(R.menu.menu_purchase);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        initview();
        initclick();
    }
    public void initview(){
        imageList.add("http://img1.imgtn.bdimg.com/it/u=932695849,3126690420&fm=21&gp=0.jpg");
        imageList.add("http://img55.it168.com/ArticleImages/fnw/2016/0426/97642e6b-4b15-496e-b963-81f9d385c139.jpg");
        cycleViewPager = (CycleViewPager) getFragmentManager()
                .findFragmentById(R.id.work_purchase_fragment);
        adapter = new PurchaseAdapter(this);
        purchase_electronic.setAdapter(adapter);
        purchase_office.setAdapter(adapter);
        initialize();
        configImageLoader();
    }
    public void initclick(){
        purchase_electronic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(PurchaseWorkActivity.this,PurchaseDetailActivity.class));
            }
        });
    }
    private void initialize() {
        for(int i = 0; i < imageList.size(); i ++){
            ADInfo info = new ADInfo();
            info.setUrl(imageList.get(i));
            info.setContent("图片-->" + i );
            infos.add(info);
        }
        // 将最后一个ImageView添加进来
        views.add(ViewFactory.getImageView(this, infos.get(infos.size() - 1).getUrl()));
        for (int i = 0; i < infos.size(); i++) {
            views.add(ViewFactory.getImageView(this, infos.get(i).getUrl()));
        }
        // 将第一个ImageView添加进来
        views.add(ViewFactory.getImageView(this, infos.get(0).getUrl()));

        // 设置循环，在调用setData方法前调用
        cycleViewPager.setCycle(true);

        // 在加载数据前设置是否循环
        cycleViewPager.setData(views, infos, mAdCycleViewListener);
        //设置轮播
        cycleViewPager.setWheel(true);

        // 设置轮播时间，默认5000ms
        cycleViewPager.setTime(2000);
        //设置圆点指示图标组居中显示，默认靠右
        cycleViewPager.setIndicatorCenter();
    }
    private CycleViewPager.ImageCycleViewListener mAdCycleViewListener = new CycleViewPager.ImageCycleViewListener() {

        @Override
        public void onImageClick(ADInfo info, int position, View imageView) {
            if (cycleViewPager.isCycle()) {

            }

        }

    };

    /**
     * 配置ImageLoder
     */
    private void configImageLoader() {
        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    public void onClick(View v) {

    }
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_addtalk:
                    startActivity(new Intent(PurchaseWorkActivity.this,ShopCartActivity.class));
                    break;
                case R.id.action_search:

                    break;
            }
            return true;
        }
    };
}
